package com.dingding.manager.core.util;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.log4j.Logger;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectResult;

import sun.misc.BASE64Decoder;

/**
 * 
 * @ClassName: UploadUtil
 * @Description: 图片上传
 * @author cheshaowei
 * @date 2015-11-16 下午05:42:47
 */
public class UploadUtil {
	
	private Logger log = Logger.getLogger(this.getClass());
	private static OSSClient client = null;
	
	static{
		//初始化OSSCLient
		client = new OSSClient(UploadConfig.getEndpoint(),
				UploadConfig.getAccesskeyid(),
				UploadConfig.getAccesskeysecret());
	}
	/**
	 * 
	 * @Title: upload
	 * @Description:上传图片，使用者处理异常
	 * @param String
	 * @author cheshaowei
	 * @throws
	 */
	public  boolean upload(String uploadData, String name) throws IOException {

		byte[] data = baseConvert(uploadData);
		//拼装图片名称
		name = name+".jpg";
		// 获取指定文件的输入流
		InputStream content = new ByteArrayInputStream(data);
		// 创建上传Object的Metadata
		ObjectMetadata meta = new ObjectMetadata();
		// 必须设置ContentLength
		meta.setContentLength(content.available());
		//设置上传格式
		meta.setContentType("image/jpeg");
		// 不使用缓存
		meta.setCacheControl("no-cache");
		// 上传Object.
		PutObjectResult result = client.putObject(UploadConfig.getBucketName(), name,
				content, meta);
		//上传失败
		if (result != null && ToolUtil.isEmpty(result.getETag())) {
			return false;
		}else{
			return true;
		}

	}

	/**
	 * @throws IOException
	 * 
	 * @Title: baseConvert
	 * @Description:将base64字符串转换成byte字节
	 * @author cheshaowei
	 * @throws
	 */
	private byte[] baseConvert(String uploadData) throws IOException {

		BASE64Decoder decoder = new BASE64Decoder();
		// Base64解码
		byte[] data = decoder.decodeBuffer(uploadData);
		for (int i = 0; i < data.length; ++i) {
			if (data[i] < 0) {// 调整异常数据
				data[i] += 256;
			}
		}
		return data;
	}
	
	
	/**
	 * @Title: uploadFileToServer
	 * @Description: 上传File对象到服务器
	 * @param  file 被上传的文件
	 * @param  name 文件的名称
	 * @author fengshuonan
	 * @return boolean true 上传成功  false 上传失败
	 * @throws
	 */
	public boolean uploadFileToServer(File file,String name)
	{
		try {
			// 获取指定文件的输入流
			InputStream content = new FileInputStream(file);
			// 创建上传Object的Metadata
			ObjectMetadata meta = new ObjectMetadata();
			// 必须设置ContentLength
			meta.setContentLength(content.available());
			//设置上传格式
			meta.setContentType("image/jpeg");
			// 不使用缓存
			meta.setCacheControl("no-cache");
			// 上传Object.
			PutObjectResult result = client.putObject(UploadConfig.getBucketName(), name,
					content, meta);
			//上传失败
			if (result != null && ToolUtil.isEmpty(result.getETag())) {
				return false;
			}else{
				return true;
			}
			
		} catch (IOException e) {
			
			log.error("上传图片出错！UploadUtil : uploadFileToServer()",e);
			return false;
		}
	}
	
	/**
	 * @Title: uploadFileToServer
	 * @Description: 上传File对象到服务器
	 * @param  file 被上传的文件
	 * @param  name 文件的名称
	 * @author fengshuonan
	 * @return boolean true 上传成功  false 上传失败
	 * @throws
	 */
	public boolean uploadFileToServer(InputStream inputStream,String name)
	{
		try {
			// 创建上传Object的Metadata
			ObjectMetadata meta = new ObjectMetadata();
			// 必须设置ContentLength
			meta.setContentLength(inputStream.available());
			//设置上传格式
			meta.setContentType("image/jpeg");
			// 不使用缓存
			meta.setCacheControl("no-cache");
			// 上传Object.
			PutObjectResult result = client.putObject(UploadConfig.getBucketName(), name,
					inputStream, meta);
			//上传失败
			if (result != null && ToolUtil.isEmpty(result.getETag())) {
				return false;
			}else{
				return true;
			}
			
		} catch (IOException e) {
			
			log.error("上传图片出错！UploadUtil : uploadFileToServer()",e);
			return false;
		}
		
	}

}
